<?php
namespace Home\Controller;
use Think\Controller;
class ShopController extends Controller
{
    /**
     * 商城首页
     * @author 佛祖<381361943@qq.com>
     */
    public function index()
    {
        $p = I('get.p',1);
        if($p  < 1)
            $p = 1;
        //查询分类
        $type = M('type')->order("`order` asc")->select();
        $this->assign('type',$type);
        $type = I('get.type');
        //商城的分类
        if($type != '')
        {
            $where['dc_type.id'] = array('eq',$type);
        }
        //最新 最热
        $newhot = I('get.newhot');
        if($newhot != '')
        {
            if($newhot == 1)
            {
                //最新
                $order ="dc_shop.id desc";
            }
            else
            {
                //最热
                $order ="dc_shop.volume desc";
            }
        }
        else
        {
            //正序排序
            $order ="dc_shop.id asc";
        }
        $count = M('shop')
            ->join("dc_type on dc_shop.type = dc_type.id")
            ->where($where)
            ->count();
        $pages = ceil($count/12);
        if($p >$pages)
        {
            $p = $pages;
        }
        $page = paging($p,1,12,$pages);
        $list = M('shop')
            /*->field("dc_shop.id,dc_shop.title,dc_type.name as type_name")*/
            ->field("dc_shop.id,dc_type.name,dc_shop.picture")
            ->join("dc_type on dc_shop.type = dc_type.id")
            ->where($where)
            ->page($p.',12')
            ->order($order)
            ->select();
        $this->assign('list',$list);
        $this->assign('page',$page['page']);
        $this->display();
    }
    /**
     * 商城详情页
     * @author 佛祖<381361943@qq.com>
     */
    public function detail()
    {
        $id = I('get.id','');
        if($id == '')
            $this->error('参数错误');
        $list = M('shop')->find($id);
        if(!$list)
        {
            $this->error('数据不存在');
        }
        //增加浏览量
        M('shop')->where(array('id'=>$id))->setInc('volume');
        $this->assign('list',$list);
        $this->display();
    }
}